%Laplacian pyramid
%Each level "i" of the Laplacian pyramid is basically a difference between  
%Gaussian level i and upsampled Gaussian level i+1.
%Note: quite a few operations are similar to the ones in Gaussian pyramid,
%so look there if you need further comments and explanations of the 
%operations
%Arguments: image I, its height and width

%This function was written after studying T. Mertens', J. Kautz' and 
%F. Van Reeth's implementation of Exposure Fusion
%http://research.edm.uhasselt.be/~tmertens/exposure_fusion/

function pyramid = laplace_pyramid(I,rows,cols)
  
numL = floor(log2(min(rows,cols)));

pyramid = cell(numL,1);
I2 = I; %I2 will contain the current level image, I will contain the
%downsampled image
for i = 1:numL - 1 
    %downsample
    I = resample2D(I,'down'); 
    
    odd = 2*size(I) - size(I2);  %odd is a vector which has 2 values, 
    %indicating whether the future upsampled dimension has to be odd (1) or
    %even (0)
    
    %calculate the difference between level l and upsampled level l+1:
    pyramid{i} = I2 - resample2D(I,'up',odd);
    I2 = I;
end
pyramid{numL} = I2; %since we can't downsample and subtract anymore, the 
%last level contsins the l-times downsampled and low-passed residual image

  


